package com.nanxun.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanxun.domain.MemberCollection;
import com.nanxun.domain.Prod;
import com.nanxun.model.Result;
import com.nanxun.service.MemberCollectionService;
import com.nanxun.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import kotlin.internal.PureReifiable;
import org.apache.xmlbeans.impl.jam.mutable.MClass;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * 会员收藏商品业务控制层
 * @param
 * @return
 * @author nanxun
 * @create 2025/6/22
 **/

@Api(tags = "会员收藏商品接口管理")
@RequestMapping("p/collection")
@RestController
public class MemberCollectionController {

    @Resource
    private MemberCollectionService memberCollectionService;

    
    
    /**
     * 查询会员收藏商品数量
     *
     * @return com.nanxun.model.Result<java.lang.Long>
     * @author nanxun
     * @create 2025/6/22
     **/
    
    // p/collection/count
    @ApiOperation("查询会员收藏商品数量")
    @GetMapping("count")
    public Result<Long> loadMemberCollectionProdCount(){
        Long count = memberCollectionService.queryMemberCollectionProdCount();
        return Result.success(count);
    }

    /**
     *
     * 分页查询会员收藏商品列表
     * @param current 页码
     * @param size 每页显示条数
     * @return com.nanxun.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.nanxun.domain.Prod>>
     * @author nanxun
     * @create 2025/6/22
     **/

    // p/collection/prods?current=1&size=10
    @ApiOperation("分页查询会员收藏商品列表")
    @GetMapping("prods")
    public Result<Page<Prod>> loadMemberCollectionProdPage(@RequestParam Long current,
                                                           @RequestParam Long size
                                                           ){
        //根据会员openid分页查询会员收藏商品列表
        Page<Prod> page = memberCollectionService.queryMemberCollectionProdPageByOpenId(AuthUtils.getMemberOpenId(),current,size);
        return Result.success(page);
    }
    /// ////////////////////微信小程序数据接口//////////////////
    /**
     *
     * 小程序查询会员收藏商品状态
     * @param prodId 商品id
     * @return com.nanxun.model.Result<java.lang.Boolean>
     * @author nanxun
     * @create 2025/6/24
     **/

    // p/collection/isCollection?prodId=103
    @ApiOperation("小程序查询会员收藏商品状态")
    @GetMapping("isCollection")
    public Result<Boolean> loadMemberIsCollection(@RequestParam Long prodId){
        long count = memberCollectionService.count(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, AuthUtils.getMemberOpenId())
                .eq(MemberCollection::getProdId, prodId)
        );
        return Result.success(1==count);
    }

    /**
     *
     * 添加或取消收藏商品
     * @param prodId 商品id
     * @return com.nanxun.model.Result<java.lang.String>
     * @author nanxun
     * @create 2025/6/24
     **/

    // p/collection/addOrCancel
    @ApiOperation("添加或取消收藏商品")
    @PostMapping("addOrCancel")
    public Result<String> addOrCancelMemberCollection(@RequestBody Long prodId){
        Boolean flag = memberCollectionService.addOrCancelMemberCollection(AuthUtils.getMemberOpenId(), prodId);
        return Result.handle(flag);
    }

}
